package com.bookstore.back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bookstore.back.entity.Bestseller;
import com.bookstore.back.entity.Book;
import com.bookstore.back.entity.BookCategory;
import com.bookstore.back.service.BestsellerScheduleService;
import com.bookstore.back.service.BestsellerService;
import com.bookstore.back.service.BookService;
import com.bookstore.back.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 畅销榜单定时更新服务实现类
 * 
 * @author 程序猿_Ti
 * @since 2025-07-22
 */
@Service
public class BestsellerScheduleServiceImpl implements BestsellerScheduleService {

    @Autowired
    private BookService bookService;
    
    @Autowired
    private BestsellerService bestsellerService;
    
    @Autowired
    private CategoryService categoryService;

    @Override
    @Transactional
    public void generateBestsellerRankings() {
        bestsellerService.remove(new QueryWrapper<>());
        generateOverallRanking("monthly", 100);

        List<BookCategory> categories = categoryService.list();
        for (BookCategory category : categories) {
            if (category.getId() != 1) {
                generateCategoryRanking(category.getId(), "monthly", 50);
            }
        }
    }

    @Override
    @Transactional
    public void generateOverallRanking(String period, int limit) {
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1)
                   .gt("sales", 0)
                   .orderByDesc("sales")
                   .last("LIMIT " + limit);

        List<Book> topBooks = bookService.list(queryWrapper);

        for (int i = 0; i < topBooks.size(); i++) {
            Book book = topBooks.get(i);
            Bestseller bestseller = new Bestseller();
            bestseller.setBookId(book.getId());
            bestseller.setCategoryId(-1); // 综合榜使用-1标识，避免与分类榜冲突
            bestseller.setRank(i + 1);
            bestseller.setPeriod(period);
            bestsellerService.save(bestseller);
        }
    }

    @Override
    @Transactional
    public void generateCategoryRanking(Integer categoryId, String period, int limit) {
        // 清除现有的分类榜单
        QueryWrapper<Bestseller> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.eq("category_id", categoryId).eq("period", period);
        bestsellerService.remove(deleteWrapper);

        // 获取该分类下销量最高的图书
        QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1) // 只统计上架的图书
                   .eq("category_id", categoryId) // 限定分类
                   .gt("sales", 0) // 只统计有销量的图书
                   .orderByDesc("sales") // 按销量倒序
                   .last("LIMIT " + limit);

        List<Book> topBooks = bookService.list(queryWrapper);

        // 生成新的分类榜单
        for (int i = 0; i < topBooks.size(); i++) {
            Book book = topBooks.get(i);
            Bestseller bestseller = new Bestseller();
            bestseller.setBookId(book.getId());
            bestseller.setCategoryId(categoryId);
            bestseller.setRank(i + 1);
            bestseller.setPeriod(period);
            bestsellerService.save(bestseller);
        }
    }

    @Override
    @Transactional
    public void cleanExpiredRankings(String period) {
        // 这里可以根据需要清理过期的榜单数据
        // 例如：清理30天前的日榜数据等
        // 目前保留所有数据，可根据实际需求调整
    }
    
}
